# coding=utf-8

# import servers
import traceback

from tests.modules.ucloud.servers import servers
from tests.modules.ucloud.ucloud_db import UcloudZone, UcloudApp


def deal_all_db(_ip):
    parent = 'uc.auaws.area'
    title = '澳洲存储仓'
    zone = 'uc.auaws.warehouse.auss'
    zone_alias = 'auss'
    zone_alias_upper = 'AUSS'
    preport = 37

    ucZone = UcloudZone(ip=_ip)

    ucApp = UcloudApp(ip=_ip)

    ucZone.do_insert(zone_code=zone,
                     zone_alias=zone,
                     parent_code=parent,
                     title='澳洲存储仓',
                     order=20)

    ucZone.commit()

    ucApp.do_insert(app_name='cwm',
                    zone=zone,
                    zone_alias=zone_alias,
                    port=preport * 1000 + 580,
                    app_type='tomcat',
                    db_type='mysql',
                    db_name='cwm_%s' % zone_alias,
                    mem_level=1)
    ucApp.commit()

    ucApp.do_insert(app_name='dep',
                    zone=zone,
                    zone_alias=zone_alias,
                    port=preport * 1000 + 380,
                    app_type='tomcat',
                    db_type='oracle',
                    db_name='OWMS_%s' % zone_alias_upper,
                    mem_level=1,
                    on_beetle='false')
    ucApp.commit()

    ucApp.do_insert(app_name='owms',
                    zone=zone,
                    zone_alias=zone_alias,
                    port=preport * 1000 + 280,
                    app_type='adempiere',
                    db_type='oracle',
                    db_name='OWMS_%s' % zone_alias_upper,
                    mem_level=1)
    ucApp.commit()

    ucApp.do_insert(app_name='SMQ',
                    zone=zone,
                    zone_alias=zone_alias,
                    port=preport * 1000 + 480,
                    app_type='php',
                    db_type=None,
                    db_name='',
                    mem_level=1,
                    on_beetle='false')
    ucApp.commit()


for _ip in servers:
    try:
        print '准备刷新ucloud%s:' % _ip

        deal_all_db(_ip=_ip);
        print 'ucloud%s刷新完成:' % _ip

    except BaseException, ex:
        print traceback.format_exc()
